<?php
// app/Model/User.php
App::uses('AppModel', 'Model');
App::uses('SimplePasswordHasher', 'Controller/Component/Auth');
class User extends AppModel
{
	public $name = 'User';
    public $hasMany = array(
        'Post' => array(
            'className' => 'Post',
            'foreignKey' => 'user_id',
            'dependent' => true,
            )
        );

    public $validate = array(
        'username' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'A username is required'
            ),
            'minlength' => array(
                'rule' => array('minLength', 4),
                'message' => 'minLength 4'
            ),
            'duplicate'  => array(
                'rule' => 'isUnique',
                'message' => 'Your username already exists'   
            ),
        ),
        'password' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'A password is required',
                'on'      => 'create'
            )
        ),
        'repass' => array(
            'equaltofield' => array(
            'rule' => array('equaltofield','password'),
            'message' => 'Require the same value to password.',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            )
        ),
        'email' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'A email is required'
            ),
            'email' => array(
                'rule' => array('email'),
                'message' => 'Please enter a valid email.'
            ),
            'duplicate'  => array(
                'rule' => 'isUnique',
                'message' => 'Your email already exists'   
            ),
        ),
        'firstname' => array(
            'required' => array(
                'rule' => 'notEmpty',
                'message' => 'Your firstname is required'
                ),
            ),
        'lastname' => array(
            'required' => array(
                'rule' => 'notEmpty',
                'message' => 'Your firstname is required'
                ),
            ),
        'role' => array(
            'valid' => array(
                'rule' => array('inList', array('admin', 'author')),
                'message' => 'Please choose a valid role',
                'allowEmpty' => false
            )
        )
    );
    
    public function equaltofield($check,$otherfield)
    {
        //get name of field
        $fname = '';
        foreach ($check as $key => $value){
            $fname = $key;
            break;
        }
        return $this->data[$this->name][$otherfield] === $this->data[$this->name][$fname];
    } 

    public function duplicate($check)
    {
        $check[] = array('id <>' . $this->data[$this->name]['id']);
        $check = $this->find('count', array('conditions'    => $check, 'recursive'  => -1));
        return ($check == 0);
    }

    public function beforeSave($options = array()) {
        if (isset($this->data[$this->alias]['password'])) {
            $passwordHasher = new SimplePasswordHasher();
            $this->data[$this->alias]['password'] = $passwordHasher->hash(
                $this->data[$this->alias]['password']
            );
        }
        return true;
    }
}